<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/css/style.css" type="text/css" media="all" />
<link rel="stylesheet" href="<?php echo URL::base() ?>media/vendor/artdialog/skins/default.css" type="text/css" media="all" />
<link href="<?php echo URL::base() ?>media/vendor/css/ui-lightness/jquery-ui-1.10.4.custom.css" rel="stylesheet">
<link href="<?php echo URL::base() ?>media/vendor/fixtable/css/component.css" rel="stylesheet">
<link href="<?php echo URL::base() ?>media/vendor/fixtable/css/normalize.css" rel="stylesheet">
<link href="<?php echo URL::base() ?>media/vendor/css/jquery-ui-timepicker-addon.min.css" rel="stylesheet">
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-1.10.2.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-1.10.4.custom.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-addon.min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery-ui-timepicker-zh-CN.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/jquery.ui.datepicker-zh-TW.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/js/Validform_v5.3.2_ncr_min.js"></script>
<script src="<?php echo URL::base() ?>media/vendor/artdialog/jquery.artDialog.js"></script>	     
<style>
.mypage{display: none;} 
.mypage .num{color: red;}
</style>
<script type="text/javascript">
//ajax 请求数据
url='';
function get_pandian(url){
    var dialog = art.dialog({title: '提示',icon:'question',lock: true});
    dialog.content('<p>数据查询中,请稍后...<p>');
    $.get(url, {
        sku: $(".form-horizontal input[name='sku']").val(),
        warehouseid : $("#warehouseid").val(),
        export_tax_rebate : $(".form-horizontal select[name='export_tax_rebate']").val(),
        enterprise_dominant : $(".form-horizontal select[name='enterprise_dominant']").val(),
        username: $(".form-horizontal input[name='username']").val(),
        supplier:$(".form-horizontal input[name='supplier']").val(),
        per_page: $(".form-horizontal select[name='per_page']").val(),
        sortcondition: $(".form-horizontal select[name='sortcondition']").val(),
        type: $(".form-horizontal select[name='type']").val(),
        //  supplier:$("input[name='supplier']").val(),
        order: $(".form-horizontal select[name='order']").val(),
        start_time: $(".form-horizontal input[name='start_time']").val(),
        end_time: $(".form-horizontal input[name='end_time']").val(),
        order_id:$(".form-horizontal input[name='order_id']").val()
    }, function(data){
        $('#table_content').empty();
        $('#table_content').html(data);
        $('#pager_links').empty();
        $('.mypage').clone().appendTo('#pager_links').show();
        dialog.close();
    });
}
//var availableTags='';
$(function() {
    var startDateTextBox = $('#start');
    var endDateTextBox = $('#end');
    startDateTextBox.datetimepicker({ 
        dateFormat: 'yy-mm-dd',
        timeFormat: 'HH:mm:ss',
        onClose: function(dateText, inst) {
            if (endDateTextBox.val() != '') {
                var testStartDate = startDateTextBox.datetimepicker('getDate');
                var testEndDate = endDateTextBox.datetimepicker('getDate');
                if (testStartDate > testEndDate)
                    endDateTextBox.datetimepicker('setDate', testStartDate);
            } else {
                endDateTextBox.val(dateText);
            }
        },
        onSelect: function (selectedDateTime){
            endDateTextBox.datetimepicker('option', 'minDate', startDateTextBox.datetimepicker('getDate') );
        }
    });
    endDateTextBox.datetimepicker({ 
        dateFormat: 'yy-mm-dd', 
        timeFormat: 'HH:mm:ss',
        onClose: function(dateText, inst) {
            if (startDateTextBox.val() != '') {
                var testStartDate = startDateTextBox.datetimepicker('getDate');
                var testEndDate = endDateTextBox.datetimepicker('getDate');
                if (testStartDate > testEndDate)
                    startDateTextBox.datetimepicker('setDate', testEndDate);
            } else {
                startDateTextBox.val(dateText);
            }
        },
        onSelect: function (selectedDateTime){
            startDateTextBox.datetimepicker('option', 'maxDate', endDateTextBox.datetimepicker('getDate') );
        }
    });
    
    $("#search").click(function(){
        url='<?php echo URL::base() ?>inventory/report/out_search';
        get_pandian(url);
    });

    function DateToUnix(string) {
        var f = string.split(' ', 2);
        var d = (f[0] ? f[0] : '').split('-', 3);
        var t = (f[1] ? f[1] : '').split(':', 3);
        return (new Date(
            parseInt(d[0], 10) || null,
            (parseInt(d[1], 10) || 1) - 1,
            parseInt(d[2], 10) || null,
            parseInt(t[0], 10) || null,
            parseInt(t[1], 10) || null,
            parseInt(t[2], 10) || null
        )).getTime() / 1000;
    }

    $("#newDown").click(function(){
        var sku=$(".form-horizontal input[name='sku']").val();
        var warehouseid = $("#warehouseid").val();
        var export_tax_rebate = $(".form-horizontal select[name='export_tax_rebate']").val();
        var enterprise_dominant = $(".form-horizontal select[name='enterprise_dominant']").val();
        var username= $(".form-horizontal input[name='username']").val();
        var supplier = $(".form-horizontal input[name='supplier']").val();
        var type= $(".form-horizontal select[name='type']").val();
        var sortcondition= $(".form-horizontal select[name='sortcondition']").val();
        var	order= $(".form-horizontal select[name='order']").val();
        var	start_time= $(".form-horizontal input[name='start_time']").val();
        var	end_time= $(".form-horizontal input[name='end_time']").val();
        var uri=url.split('?')[1];
        if(DateToUnix(end_time) - DateToUnix(start_time) >= 86400) {
            if(!window.confirm("时间区间偏大数据量会非常大Execel可能支持不了，请确认好筛选条件，确定下载吗？")) {
                return false;
            }
        }
        if(!uri){
            uri = 'sku=' + sku + '&username=' + username + '&supplier=' + supplier + '&type=' + type + '&sortcondition=' + sortcondition + '&order=' + order
                + '&start_time=' + start_time + '&end_time=' + end_time +'&warehouseid=' + warehouseid
                + '&enterprise_dominant=' + enterprise_dominant;
        }
        /**
        $.get("<?php echo url::base(true);?>inventory/report/newDownloadOut?" + uri,{}, function (data) {
            window.open("<?php echo url::base(true);?>disk/"+data);
        });
        */
        window.location.href = "<?php echo URL::base()?>inventory/report/newDownloadOut?"+uri;
    });

    $("#down").click(function(){
        var num=$('#pager_links .num').text();
        if(num>5000){
            var dialog = art.dialog({title: '提示',icon:'question',lock: true});
            dialog.content('<p>你下载的量超过5000条，请加大筛选条件力度<p>');
            return;
        }
        var sku=$(".form-horizontal input[name='sku']").val();
        var username= $(".form-horizontal input[name='username']").val();
        var type= $(".form-horizontal select[name='type']").val();
        var sortcondition= $(".form-horizontal select[name='sortcondition']").val();
        var	order= $(".form-horizontal select[name='order']").val();
        var	start_time= $(".form-horizontal input[name='start_time']").val();
        var	end_time= $(".form-horizontal input[name='end_time']").val();
        var uri=url.split('?')[1];
        if(!uri){
            uri='sku='+sku+'&username='+username+'&type='+type+'&sortcondition='+sortcondition+'&order='+order+'&start_time='+start_time+'&end_time='+end_time;
        }
        window.location.href="<?php echo URL::base()?>inventory/report/down_out?"+uri;
    });
    
    $(document).on('click', '.pagination a', function(e){
        e.preventDefault(); 
        url =$(this).attr('href');
        if(url.length){
            get_pandian(url);
        }
    });

    // 查询供应商自动提示补全
    $("#supplier_name").autocomplete({
       autoFocus: true,
       minLength: 0,
       autoFill: true,
       source: '<?php echo URL::base(true)?>purchase/get_supplier',
       select: function (event, ui) {
           //从建议列表中选中之后触发的事件
           $("#supplier_name").val(ui.item.name);
           return false;
       }
    });
});
</script>

<form class="form-horizontal" id="postform">
    <div class="control-group">
        <label class="control-label w-label">仓库ID: </label>
        <div class="controls">
            <select name="warehouseid" id="warehouseid">
                <option value="-1">全部</option>
                <?php if(isset($warehouses) and $warehouses){
                    foreach($warehouses as $k => $warehouse){
                        ?>
                        <option value="<?php echo $warehouse['id']; ?>"><?php echo $warehouse['name']; ?></option>
                    <?php }
                } ?>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label w-label">SKU:</label>
        <div class="controls">
            <input type="text" name='sku' value=""> 
        </div>
    </div>
    <div class="control-group">
        <label class="control-label w-label">供应商:</label>
        <div class="controls">
            <input type="text" id="supplier_name" name='supplier' value="">
        </div>
    </div>
    <div class="control-group adv hidden">
        <label class="control-label w-label">订单号:</label>
        <div class="controls">
            <input type="text" name='order_id' value="">
        </div>
    </div>
    <div class="control-group adv hidden">
        <label class="control-label w-label">出库类型:</label>
        <div class="controls">
            <select  name='type'>
                <option value="">选择（默认全部）</option>
                <?php foreach($deliveryTypes as $number=>$value){?>
                    <option value='<?php echo $number;?>'><?php echo $value;?></option>
                <?php }?>
            </select>
        </div>
    </div>
    <div class="control-group adv hidden">
        <label class="control-label w-label">公司主体: </label>
        <div class="controls">
            <select name="enterprise_dominant" id="enterprise_dominant">
                <option value="-1">全部</option>
                <?php if(isset($enterprise_dominant) and $enterprise_dominant){
                    foreach($enterprise_dominant as $k => $v){
                        ?>
                        <option value="<?php echo $k ?>">
                            <?php echo isset($enterprise_dominant[$k])?$enterprise_dominant[$k]:'' ?>
                        </option>
                    <?php }
                } ?>
            </select>
        </div>
    </div>
    <div class="control-group adv hidden">
        <label class="control-label w-label">操作员:</label>
        <div class="controls">
            <input type="text"   name='username' value="">
        </div>
    </div>
    <div class="control-group adv hidden">
        <label class="control-label w-label">自定义页数:</label>
        <div class="controls">
            <select  name='per_page'>
                <option value="">选择（默认15）</option>
                <option value='15'>15</option>
                <option value='30'>30</option>
                <option value='50'>50</option>
            </select>
        </div>
    </div>
    <div class="control-group hidden">
        <label class="control-label w-label">排序条件:</label>
        <div class="controls">
            <select  name='sortcondition'>
                <option value="3">选择（默认出库时间）</option>
                <option value='1'>SKU</option>
                <option value='2'>供应商</option>
                <option value='3'>出库时间</option>
            </select>
        </div>
    </div>
    <div class="control-group hidden">
        <label class="control-label w-label">排序方式:</label>
        <div class="controls">
            <select name='order'>
                <option value='2'>降序</option>
                <option value='1'>升序</option>
            </select>
        </div>
    </div>
    <div class="control-group">
        <label class="control-label w-label" for="inputPassword">出库日期:</label>
        <div class="controls ">
            <input type="text" name="start_time" id="start"> ~ 
            <input type="text" name="end_time" id="end">
        </div>
        <div style=" margin-top:10px;">
            <div class="btn btn-info" id="search" style="margin-left:170px;">查询</div>
            <input type="reset" style="margin-left:10px;" class="btn" value="重置">
            <div class="btn" id='newDown' style="margin-left:10px">下载</div>
            <a href="javascript:;" class="btn btn-link" onclick="javascript:$('#postform .adv').toggleClass('hidden');">更多筛选条件</a>
        </div>
    </div>
</form>
<table class="table table-hover" style="border-collapse: collapse; margin-top: 15px;">
    <thead>
        <tr>
            <th>出库单号</th>
            <th>SKU</th>
            <th>中文名称</th>
            <th>出库量</th>
            <th>仓库</th>
            <th>出库时间</th>
            <th>出库类型</th>
            <th>供应商</th>
            <th>公司主体</th>
            <th>储位</th>
            <th>订单号</th>
            <th>销售单价</th>
            <th>销售金额</th>
            <th>成本金额</th>
            <th>备注</th>
            <th>操作人</th>
        </tr>
    </thead>
    <tbody id='table_content'></tbody>
</table>
<div id="pager_links"></div>

